package com.school.hjt.schoolclasscard.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.school.hjt.schoolclasscard.R;
import com.school.hjt.schoolclasscard.db.CardStudent;
import com.school.hjt.schoolclasscard.utlis.ClassCalendar;
import com.school.hjt.schoolclasscard.utlis.Constant;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.List;

/**
 * Created by xqx on 2017/10/25.
 * 照片墙
 */

public class StudentFaceAdapter extends BaseAdapter {
    private Context mContext;
    private List<CardStudent> list;
    public StudentFaceAdapter(Context mContext, List<CardStudent> list) {
        this.mContext = mContext;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHander viewHander;
        if (convertView==null){
            viewHander=new ViewHander();
            convertView=View.inflate(mContext, R.layout.face_item,null);
            viewHander.iv= convertView.findViewById(R.id.iv_Face);
            viewHander.face=  convertView.findViewById(R.id.Face);
            viewHander.tv_nameface=  convertView.findViewById(R.id.tv_nameFace);
            viewHander.tv_noFace= convertView.findViewById(R.id.tv_noFace);
            convertView.setTag(viewHander);

        }else{
            viewHander= (ViewHander) convertView.getTag();
        }
        CardStudent classKaoQinDetail = list.get(position);
        Glide.with(mContext).load(classKaoQinDetail.getStudent_url()).error(R.mipmap.student).listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                notifyDataSetChanged();
                return false;
            }
        }).into(viewHander.iv);
        int signtype = classKaoQinDetail.getStudentSign().getSigntype();
        if (signtype==0){
            viewHander.tv_noFace.setText("人脸识别失败");
        }else if(signtype==3){
            viewHander.tv_noFace.setText("人脸识别中");
        }
        File file=new File(Environment.getExternalStorageDirectory().getPath()+"/Face/"+ classKaoQinDetail.getCard_no()+ ClassCalendar.getIntDay()+".te");
        if(file.exists()){
            Bitmap diskBitmap = getDiskBitmap(file.getAbsolutePath());
            viewHander.face.setImageBitmap(diskBitmap);
        }
//        if (classKaoQinDetail.getImg_id()!=null){
//            Log.e("ATG","进来"+classKaoQinDetail.getImg_id());
//            Bitmap bitmap = decodeImg(classKaoQinDetail.getImg_id());
//            viewHander.face.setImageBitmap(bitmap);
//        }
        viewHander.tv_nameface.setText(classKaoQinDetail.getStudent_name());
        return convertView;
    }
    class ViewHander{
        private ImageView iv,face;
        private TextView tv_nameface,tv_noFace;

    }
    private Bitmap getDiskBitmap(String pathString)
    {
        Bitmap bitmap = null;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathString, opts);
        opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128);
        opts.inJustDecodeBounds = false;
        try {
            bitmap = BitmapFactory.decodeFile(pathString, opts);
//            imageView.setImageBitmap(bmp);
        } catch (OutOfMemoryError err) {
        }
        return bitmap;
    }
    private  int computeInitialSampleSize(BitmapFactory.Options options,
                                          int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;
        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
                .sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
                Math.floor(w / minSideLength), Math.floor(h / minSideLength));
        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }
        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
    public  int computeSampleSize(BitmapFactory.Options options,
                                  int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength,
                maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }

}
